<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/10/19
 * Time: 下午3:40
 */
class MaintenanceEngineers extends Eloquent
{
    protected $table = "maintenance_engineers";

    public static function getEngineerByToken($token,&$response){
        $engineer_token = EngineerTokens::where('token',$token)->first();
        if(is_null($engineer_token)){
            $response = Lang::get("sce_error.user_token_not_exist_1004");
            return NULL; //token不存在
        }else{
            $engineer_data = MaintenanceEngineers::where('id',$engineer_token->engineer_id)
                ->where('status',1)
                ->first();
            if(is_null($engineer_data)){
                $response = Lang::get("sce_error.user_not_exist_1024");
                return null; //用户不存在
            }else{

                $ms=MaintenanceShops::find($engineer_data->type_id);
                if (strtotime($engineer_data->pwd_at) + $ms->password_day*24*60*60 <= time()) {  //当前时间是不是超过了门店用户的有效登陆时间
                    $response = Lang::get("sce_error.sce_user_login_error_1023");
                    return null;
                }
                return $engineer_data;
            }
        }
    }
    
    public static function getEngineerByTokenWx($token){
        $engineer_token = EngineerTokensWx::where('token',$token)->first();
        if(is_null($engineer_token)){
            return -2; //token不存在
        }else{
            // $engineer_data = MaintenanceEngineers::where('id',$engineer_token->engineer_id)
            $engineer_data = SCUser::where('id',$engineer_token->engineer_id)
            ->where('status',1)
            ->first();
            if(is_null($engineer_data)){
                return -3; //用户不存在
            }else{
                return $engineer_data;
            }
        }
    }

}